//
//  ViewController.m
//  Gesture
//
//  Created by Justsoso on 16/3/17.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    double lastDistance;
    
    CGRect originFrame;
}
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view3;
@property (weak, nonatomic) IBOutlet UIView *view4;
@property (weak, nonatomic) IBOutlet UIView *view5;
@property (weak, nonatomic) IBOutlet UIView *view6;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    originFrame = self.imageView.frame;
    
    // 创建一个手势识别点击的手势识别
    UITapGestureRecognizer *tapR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDo:)];
    tapR.numberOfTapsRequired = 5; // 连续点击次数
    tapR.numberOfTouchesRequired = 1;// 手指数量
    [self.view1 addGestureRecognizer:tapR];
    
    // 轻扫手势
    UISwipeGestureRecognizer *swipR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipDo:)];
    swipR.direction = UISwipeGestureRecognizerDirectionRight;
    swipR.numberOfTouchesRequired = 1;
    [self.view2 addGestureRecognizer:swipR];
    
    UISwipeGestureRecognizer *swipLR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipDo:)];
    swipLR.direction = UISwipeGestureRecognizerDirectionLeft;
    swipLR.numberOfTouchesRequired = 1;
    [self.view2 addGestureRecognizer:swipLR];
    
    // 捏合手势
    UIPinchGestureRecognizer *pinR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinDo:)];
    [self.view3 addGestureRecognizer:pinR];
    
    // 旋转手势
    UIRotationGestureRecognizer *rotateR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateDo:)];
    [self.view4 addGestureRecognizer:rotateR];
    
    // 拖动手势
    UIPanGestureRecognizer *panR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDo:)];
    [self.view5 addGestureRecognizer:panR];
    
    // 长按手势
    UILongPressGestureRecognizer *longR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longDo:)];
    [self.view6 addGestureRecognizer:longR];
}

- (void)tapDo:(UITapGestureRecognizer *)g
{
    NSLog(@"检测到点击手势");
}

- (void)swipDo:(UISwipeGestureRecognizer *)g
{
    NSLog(@"检测到轻扫手势");
    if (g.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        self.imageView.frame = CGRectOffset(self.imageView.frame, -20, 0);
    }
    else
    {
        self.imageView.frame = CGRectOffset(self.imageView.frame, 20, 0);
    }
}

- (void)pinDo:(UIPinchGestureRecognizer *)g
{
    NSLog(@"检测到捏合手势");
    double scale = g.scale;
    CGRect curFrame = originFrame;
    self.imageView.frame = CGRectMake(curFrame.origin.x, curFrame.origin.y, curFrame.size.width*scale, curFrame.size.height*scale);
}

- (void)rotateDo:(UIRotationGestureRecognizer *)g
{
    NSLog(@"检测到旋转手势");
    double rotation = g.rotation;
    self.imageView.transform = CGAffineTransformMakeRotation(rotation);
}

- (void)panDo:(UIPanGestureRecognizer *)g
{
    NSLog(@"检测到拖动手势");
    CGPoint tran = [g translationInView:self.view5];// 移动了多少距离
    
    CGPoint center = self.imageView.center;
    center.x += tran.x;
    center.y += tran.y;
    
    self.imageView.center = center;
    
    [g setTranslation:CGPointZero inView:self.view5];
}

- (void)longDo:(UILongPressGestureRecognizer *)g
{
    NSLog(@"检测到长按手势");
    self.imageView.frame = originFrame;
    switch (g.state) {
        case UIGestureRecognizerStateBegan:
            NSLog(@"手势开始");
            break;
        case UIGestureRecognizerStateChanged:
            NSLog(@"手势改变");
            break;
        case UIGestureRecognizerStateEnded:
            NSLog(@"手势结束");
            break;
        case UIGestureRecognizerStateCancelled:
            NSLog(@"手势中断");
            break;
        case UIGestureRecognizerStateFailed:
            NSLog(@"手势失败");
            break;
        case UIGestureRecognizerStatePossible:
            NSLog(@"手势Possible");
            break;
            
        default:
            break;
    }
}
@end
